package a51qindian.com.comprehensive.activity;

import android.widget.Button;
import android.widget.EditText;

import com.li.widget.MultipleLayout;
import com.li.widget.TopNavigationBar;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import a51qindian.com.comprehensive.R;
import a51qindian.com.comprehensive.base.QDBaseActivity;
import a51qindian.com.comprehensive.net.ObservableHelper;
import a51qindian.com.comprehensive.net.RetrofitCreator;
import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;


/**
 * Author: Li
 * Date: 2017/10/24 0024
 * Description: 添加区代
 */

public class AddAgentActivity extends QDBaseActivity {

    @BindView(R.id.top_navigation)
    TopNavigationBar mTopNavigation;
    @BindView(R.id.et_account)
    EditText mEtAccount;
    @BindView(R.id.et_pwd)
    EditText mEtPwd;
    @BindView(R.id.et_pwd_a)
    EditText mEtPwdA;
    @BindView(R.id.et_belong)
    EditText mEtBelong;
    @BindView(R.id.et_name)
    EditText mEtName;
    @BindView(R.id.btn_confirm)
    Button mBtnConfirm;
    @BindView(R.id.layout_content)
    MultipleLayout mLayoutContent;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_agent;
    }

    @Override
    protected void initSth() {
        super.initSth();
        mTopNavigation.setNavigationBarListener(this);
    }

    @OnClick(R.id.btn_confirm)
    public void onViewClicked() {
        if (checkIsEmpty(mEtAccount)||checkIsEmpty(mEtPwd)
                ||checkIsEmpty(mEtPwdA)||checkIsEmpty(mEtBelong)||checkIsEmpty(mEtName)) {
            showToast("请正确填写全部信息");
            return;
        }
        if (!mEtPwd.getText().toString().equals(mEtPwdA.getText().toString())) {
            showToast("两次密码不一样");
            return;
        }


        addAgent(mUid,mEtAccount.getText().toString(),
                mEtPwd.getText().toString(),mEtPwdA.getText().toString(),mEtBelong.getText().toString(),
                mEtName.getText().toString());
    }


    private void addAgent(String uid, String userName, String password, String rePassword, String area, String realName) {
        mLayoutContent.onLoadingWithContent("正在添加");
        mBtnConfirm.setEnabled(false);
        Map<String, String> map = new HashMap<>();
        map.put("uid", uid);
        map.put("username", userName);
        map.put("password", password);
        map.put("repassword", rePassword);
        map.put("area", area);
        map.put("realname", realName);
        ObservableHelper.createObservable(RetrofitCreator.createService().addAgent(map))
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(@NonNull String s) {
                        mLayoutContent.onSuccess();
                        mBtnConfirm.setEnabled(true);
                        try {
                            JSONObject object=new JSONObject(s);
                            showToast(object.getString("info"));
                            int code=object.getInt("code");
                            if (code==1) {
                                finish();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mLayoutContent.onSuccess();
                        showToast(e.getMessage());
                        mBtnConfirm.setEnabled(true);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
